package com.kasa.gps;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Message;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TimePicker;

import com.kasa.gps.db.table.VehiclesInfoTableAdapter;
import com.kasa.gps.service.TrackService;
import com.kasa.gps.tool.Tool;

public class AlarmActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(R.string.alarm_dialog_title)
				.setMessage(R.string.alarm_dialog_msg)
				.setPositiveButton(R.string.alarm_dialog_positivebutton_task_end, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						//如果结束任务
						//向后台服务发送数据上传的消息
						Bundle data = new Bundle();
						data.putBoolean(Tool.CLOSE_SERVICE_KEY, true);
						TrackService.TrackServiceHandler handler = new TrackService.TrackServiceHandler();
						Message msg = handler.obtainMessage(handler.WHAT_UPLOAD_0);
						msg.setData(data);
						handler.sendMessage(msg);
						
						//取消振动
						Tool.vibrateOff();
					}
				})
				.setNegativeButton(R.string.alarm_dialog_negativebutton_task_delay, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						//如果延长任务时间

						LinearLayout expectedTime_layout = new LinearLayout(AlarmActivity.this);
						expectedTime_layout.setOrientation(LinearLayout.VERTICAL);
						//日期
						final DatePicker datePicker = new DatePicker(AlarmActivity.this);
						expectedTime_layout.addView(datePicker);
						//时间
						final TimePicker timePicker = new TimePicker(AlarmActivity.this);
						timePicker.setIs24HourView(true);
						expectedTime_layout.addView(timePicker);
						
						AlertDialog.Builder builder = new AlertDialog.Builder(AlarmActivity.this);
						builder.setTitle(R.string.expectedTime_edit_dialog_title)
								.setView(expectedTime_layout)
								.setPositiveButton(R.string.dialog_positivebutton_text, new DialogInterface.OnClickListener() {
									
									@Override
									public void onClick(DialogInterface dialog, int which) {
										StringBuffer str = new StringBuffer();
										str.append(datePicker.getYear()).append(Tool.SPLIT_Y_M_D);
										if(datePicker.getMonth()+1 < 10){
											str.append("0");
										}
										str.append(datePicker.getMonth()+1).append(Tool.SPLIT_Y_M_D);
										if(datePicker.getDayOfMonth() < 10){
											str.append("0");
										}
										str.append(datePicker.getDayOfMonth()).append(Tool.SPLIT_DATE_TIME);
										if(timePicker.getCurrentHour()<10){
											str.append("0");
										}
										str.append(timePicker.getCurrentHour()).append(Tool.SPLIT_H_M);
										if(timePicker.getCurrentMinute()<10){
											str.append("0");
										}
										str.append(timePicker.getCurrentMinute());
									
										//向后台服务发送延长服务时间的消息
										Bundle data = new Bundle();
										data.putString(VehiclesInfoTableAdapter.EXPECTED_TIME, str.toString());
										TrackService.TrackServiceHandler handler = new TrackService.TrackServiceHandler();
										Message msg = handler.obtainMessage(handler.WHAT_DELAYSERVICE_1);
										msg.setData(data);
										handler.sendMessage(msg);
										
										//取消振动
										Tool.vibrateOff();
									}
								})
								.setNegativeButton(R.string.dialog_negativebutton_text, null)
								.show();
						
					
					}
				})
				.show();
	
	}
}
